package com.lss.e_array;

/**
 * @author lss
 * @description 最大连续子数组和
 * @createDate 2024/12/11-17:05
 * @tag 前缀
 */
public class lc_53 {
    public static void main(String[] args) {

    }

    /**
     * 通过前缀和的形式进行求解
     * @param nums
     * @return
     */
    public int maxSubArray(int[] nums) {
        int ans = Integer.MIN_VALUE;
        int preSum = 0;
        int minPreSum = 0;
        for (int x : nums){
            preSum += x;
            ans = Math.max(ans,preSum - minPreSum);
            minPreSum = Math.min(minPreSum,preSum);
        }
        return ans;
    }

    public int maxSubArray_0(int[] nums) {
        int[] f= new int[nums.length];
        f[0] = nums[0];
        int max = f[0];
        for (int i = 1; i < nums.length; i++) {
            f[i] = Math.max(0,f[i - 1])  + nums[i];
            max =  Math.max(f[i],max);
        }
        return max;
    }
}
